/**
 * 湖南江河机电自动化设备股份有限公司 版权所有
 * Copyright @2016-2020 All Rights Reserved
 */
package com.hnjme.cnec.mdm.config;

import com.fasterxml.classmate.TypeResolver;
import com.google.common.collect.Sets;
import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.AlternateTypeRules;
import springfox.documentation.schema.WildcardType;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

/**
 * @author outta
 * @Since 1.0 2018/8/27
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    /**
     * @return
     */
    @Bean
    protected Docket initDocket(TypeResolver typeResolver) {
        return new Docket(DocumentationType.SWAGGER_2)

                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build()
                .pathMapping("/")
                .protocols(Sets.newHashSet("https", "http"))
                .useDefaultResponseMessages(false)
                .genericModelSubstitutes(ResponseEntity.class)
                .apiInfo(createAPIInfo())
                .globalResponseMessage(RequestMethod.GET, exceptionResponseMessage())
                .alternateTypeRules(
                        AlternateTypeRules.newRule(
                                typeResolver.resolve(Collection.class, WildcardType.class),
                                typeResolver.resolve(List.class, WildcardType.class))
                )
                .forCodeGeneration(false);
    }

    /**
     * @return
     */
    protected ArrayList<ResponseMessage> exceptionResponseMessage() {
        return newArrayList(new ResponseMessageBuilder().code(200).message("操作成功").build(),
                new ResponseMessageBuilder().code(500).message("程序内部错误或请求不满足校验条件").build(),
                new ResponseMessageBuilder().code(404).message("uri错误，请求无法处理").build(),
                new ResponseMessageBuilder().code(400).message("请求参数错误").build(),
                new ResponseMessageBuilder().code(405).message("不支持的操作类型").build(),
                new ResponseMessageBuilder().code(406).message("不可接受的媒体类型").build(),
                new ResponseMessageBuilder().code(415).message("不支持的媒体类型").build(),
                new ResponseMessageBuilder().code(401).message("权限不足，缺少token").build());
    }

    /**
     * @return
     */
    protected ApiInfo createAPIInfo() {
        return new ApiInfoBuilder()
                .title("中国核工业集团主数据API")
                .description("本API服务仅为各应用系统提供集团内的统一的员工、组织机构的基础属性信息查询功能。<p>各业务系统应在各自的系统中实现员工在系统中所需要的额外属性信息的维护管理工作。<p>API接口数据每<strong><font color='red'> 2分钟 </font></strong>和主数据系统同步一次")
                .termsOfServiceUrl("http://www.hnjme.com/")
                .version("1.0")
                .build();
    }
}
